<?php

/**
 * 文件由尚数增量代码引擎生成
 * @author: Shann 851188611@qq.com
 */

namespace app\admin\controller;

class ProductBase
{
    public function index()
    {
        $products = \app\model\Product::field(['id', 'code', 'cover_file_id', 'name', 'status_value', 'price', 'created_at', 'updated_at']);

        $products->order('id', 'desc');

        $products->with(['cover' => function ($query) {
            $query->field(['id', 'url']);
        }]);

        $data['products'] = $products->select();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function detail()
    {
        $product = \app\model\Product::field(['id', 'code', 'cover_file_id', 'name', 'status_value', 'price', 'created_at', 'updated_at']);

        $product->where('id', input('id'));

        $data['product'] = $product->find();

        return json(['code' => 20000, 'data' => $data]);
    }

    public function create()
    {
        $param = request()->param(['code', 'coverFileId', 'name', 'statusValue', 'price', 'createdAt', 'updatedAt']);

        $model = new \app\model\Product();

        $model->save($param);

        $model = \app\model\Product::find($model->id);

        return json(['code' => 20000, 'msg' => 'success', 'data' => ['product' => $model]]);
    }

    public function update()
    {
        $param = request()->param();

        $model = \app\model\Product::find(input('id'));

        $model->save($param);

        return json(['code' => 20000, 'msg' => 'success', 'data' => []]);
    }

    public function delete()
    {
        \app\model\Product::destroy(function ($query) {
            $query->where('id', input('id'));
        });

        return json(['code' => 20000, 'msg' => 'success']);
    }
}
